<?php
class inStock extends Admin {

    private $tab = array( 'inStock' => '入库管理','inGoodsEit'=>'货品管理');
    private $color = array('深红', '大红','橘红','橘黄 ',
                             '玫瑰红','橙色','深黄','淡黄','草绿',
                            '墨绿', '柠檬黄', '黄绿', '蓝绿', '浅绿',
                           '天蓝', '湖蓝', '黑色', '白色', '紫色',
                           '红灰', '黄灰', '蓝灰', '紫灰', '浅灰');
    private $size = array('080CM','085CM','090CM','095CM','100CM',"105CM","110CM",
                            "115CM","120CM","125CM","130CM","135CM","140CM","145CM","150CM","155CM","160CM");

    public function __construct() {
        parent::__construct();
        $this->pk = "inOutStockNo";
    }

    public function index( $c, $a ) {
        $type = G( 'type', 'inStock' );

        $method = "{$a}_{$type}";
        $this->$method( $c, $a, $type );
    }


    private function index_inStock( $c, $a, $type ) {
        $type = G( 'type', 'inStock' );
        $this->table = 'inOutStock';


        $sql = "select  A.*,B.name from xbl_inOutStock as A,xbl_supplier as B where A.supplierId = B.id and  inOutStyle = 0";
        $result = $this->Paged()->findSql($sql);

        $pager = $result['page'];
        $result = $result['result'];



        include $this->display( $c );
    }

    private function index_inGoodsEit( $c, $a, $type ) {

        header( "location:?c=inGoodsEdit");
    }

    public function edit( $c, $a ) {
        $type = G( 'type', 'inStock' );
        if ( !$id = intval( G( 'inOutStockNo' ) ) ) {
            $this->error();
            exit;
        }

        $this->table = 'inOutStock'; // 数据库表名
        $result = $this->find( array( 'inOutStockNo' => $id ) ); // 查找数据


        $this->table = 'stockPos';
        $this->pk = 'stockNo';
        $res = $this->findAll(null,null,'stockNo,stockName');

        $this->table = 'supplier';
        $this->pk = 'id';

        $res1 = $this->findAll(null,null,'id,name');

        include $this->display( $c ); // 加载模板
    }

    # 删除商品
    public function del( $c ) {
        if ( !$id = intval( G( "inOutStockNo" ) ) ) {
            exit( '非法删除商品' );
        }

        #删除商品
        $this->table = 'inOutStock';
        $msg = $this->delete( array( "inOutStockNo" => $id ) ) ? 'success' : 'failed';

        # 删除附件
        $this->table = 'attachment';
        $this->delete( array( "aid" => $id, 'model' => strtolower( $c ) ) );
        exit( $msg );
    }

    public function search_inStock( $c, $a ) {
        $this->table = 'inOutStock'; // 数据库表名
        $type = G( 'type', 'inStock' );

        $where = array( G( 'k' ) => G( 'v' ) );
        if ( $key = G( 'key' ) ) {
            $tmp = '';
            foreach ( $where as $k => $v ) {
                $tmp .= "{$k}='" . urldecode( $v ) . "' and ";
            }
            $where = $tmp ? ( $tmp . "inOutStockNo ={$key} " ) : '';
        }

        $this->pk = 'inOutStockNo';
        $result = $this->Paged()->findAll( $where );
        $pager = $result[ 'page' ];
        $result = $result[ 'result' ];


        include $this->display( $c ); // 加载模板


    }


    public function addOrder( $c, $a) {


        $type = G( 'type', 'inStock' );

        //$this->table = 'inOutStock'; // 数据库表名
       // $result = $this->find( array( 'inOutStockNo' => $id ) ); // 查找数据


        $this->table = 'stockPos';
        $this->pk = 'stockNo';
        $res = $this->findAll(null,null,'stockNo,stockName');

        $this->table = 'supplier';
        $this->pk = 'id';

        $res1 = $this->findAll(null,null,'id,name');


        //print_r($type);
        include $this->display( $c );
    }









    public function save($c,$a) {

        $type = G("type", 'inStock');
        # 获取表单数据

        $rows[ 'inOutStockNo' ] = htmlspecialchars( trim( $_POST['orderNo'] ) );
        $rows[ 'amount' ] = htmlspecialchars( trim( $_POST['totalNum'] ) );
        if($rows['amount'] == null)
             $rows[ 'amount' ] = 0;

        /*****zouhong-edit*/
        $rows['inOutStockDate'] = date("YmdHis");
        $rows['operator'] = $this->admin['author'];
        $rows['logisticCost'] = $_POST['logisticCost'];

        $rows['stockNo'] = $_POST['stockNo'];

        /*****zouhong-edit-end*/

        $this->table = 'inOutStock';
        # 注意 表单字段必须跟数据库表字段名一样
        if($type == 'inStock')
        {
            $rows['supplierId'] =  htmlspecialchars( trim( $_POST['supplierId'] ) );
            $rows['inOutStyle'] =  0;
        }
        else
        {
            $rows['inOutStyle'] =  1;
        }

        if ( $id = $this->find(  array('inOutStockNo' => $rows[ 'inOutStockNo' ])) ) {
            $msg = $this->update( array( 'inOutStockNo' => $id['inOutStockNo'] ), $rows ) ? 'success' : 'failed';  //更新供货商
        } else {
            $msg = $this->create( $rows ) ? 'success' : 'failed';                         // 添加供货商
        }
        exit( $msg );
    }
}
